package StudyCode.summary.BufferAndChannel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReadFileUsingSeekableByteChannel {

	public static void main(String[] args) throws IOException {
		Path path = Paths.get("E:/Copy/2.txt");
		int num = seek(path);
		System.out.println("这个值是 " + num);
	}

	private static int seek(Path path) {
		int num = -1;

		try (SeekableByteChannel fc = Files.newByteChannel(path)) {

			ByteBuffer buf = ByteBuffer.allocate(8);

			fc.read(buf);
			long offset = buf.getLong(0);

			fc.position(offset);
			buf.rewind();

			fc.read(buf);
			num = buf.getInt(0);
		} catch (IOException x) {
			System.err.println(x);
		}
		return num;
	}
}
